<?php
class controller_mission extends controller_abstract
{
	function __construct(& $app)
	{
		parent::__construct($app);
		$this->mission =  loader::model('mission');
	}

	function index()
	{
		$this->view->assign('missions', table('mission'));
		$this->view->display('index');
	}

	function show()
	{
		$data = $this->mission->ls();
		$this->view->assign('data', $data);
		$this->view->display('missions');
	}
	
	function add()
	{
		if($this->is_post())
		{
			if($this->mission->add($_POST))
			{
				$this->showmessage('任务添加成功！', url('caitu/mission/show'));
			}
		}
		else
		{
			$this->view->display('add');
		}
	}
	
	function edit()
	{
		if($this->is_post())
		{
			if($this->mission->edit($_POST['missionid'],$_POST))
			{
				$this->showmessage('任务编辑成功！', url('caitu/mission/show'));
			}
		}
		else
		{
			$missionid = $_GET['missionid'];
			$r = $this->mission->get($missionid);
			$this->view->assign($r);
			$this->view->display('edit');
		}
	}
	
	function copy()
	{
		$missionid = $_GET['missionid'];
		if($this->mission->copy($missionid))
		{
			$this->showmessage('复制成功！',url('caitu/mission/index'));
		}
	}
	
	function delete()
	{
		$missionid = $_GET['missionid'];
		$r = $this->mission->delete($missionid);
		$this->redirect(url('caitu/mission/show'));
	}
	
	function fetch()
	{
		$missioid = $_GET['missionid'];
		$num = $_GET['num'];
		$r = $this->mission->get($missioid);
		if(!empty($num))
		{
			$lists = array();
			for($i=1; $i<=$num ; $i++)
			{
				$lists[] = str_replace('{num}', $i , $r['urlrule']);
			}
			cache_write('mission/'.$r['missionid'].'.php', $lists);
		}
		$this->showmessage('列表页已缓存,开始读取.......', url('caitu/mission/fetch_list',array('missionid'=>$r['missionid'])));
	}
	
	function fetch_list()
	{
		$missionid = $_GET['missionid'];
		$lists = cache_read('mission/'.$missionid.'.php');
		$total = count($lists);
		$now = isset($_GET['now']) ? $_GET['now'] : 0;
		
		$r = table('mission', $missionid);
		$urlinfo = parse_url($r['index']);
		$host = $urlinfo['scheme'].'://'.$urlinfo['host'];
		$listtype = 'uncoincide';//coincide uncoincide第一页与第二页是否重合 需要配置

		if(!$now)
		{
			$content = file_get_contents($r['index']);
		}
		else
		{
			$content = file_get_contents($lists[$r['list_coincide']?$now:$now-1]);
		}
	
		//编码转换
		$charset = $CONFIG['charset'];
		if($r['charset'] != $charset)
		{
			$content = str_charset($r['charset'], $charset, $content);
		}
		//匹配列表区域
		$listarea_reg = str2reg($r['listarea']);
		preg_match($listarea_reg, $content, $matches);
		$linkrule_reg = str2reg($r['linkrule']);
		preg_match_all($linkrule_reg, $matches[0], $links, PREG_SET_ORDER);
		//cache_write('mission/list_'.$now.'.php', $links);
		$content = loader::model('content');
		foreach ($links as $val)
		{
			$content_urlinfo = parse_url($val[1]);
			if(!isset($content_urlinfo['host']))
			{
				$val[1] = substr($val[1],0,2) == '..'?$host.substr($val[1],2):$host.$val[1];
			}
			$content->add(array(
			'missionid'=>$missionid,
			'title'=>$val[2],
			'url'=>$val[1],
			'passed'=>0));
		}
		if(!$r['list_coincide']?$now<=$total:$now<=$total)
		{
			$this->showmessage('第'.(++$now).'页采集成功！',url('caitu/mission/fetch_list',array('missionid'=>$missionid,'now'=>$now)));
		}
		else
		{
			$this->showmessage('此任务所有内容链接采集完毕！',url('caitu/mission/index'));
		}
	}
}